package com.feiyuan.movie.app;

import com.feiyuan.common.annotation.Login;
import com.feiyuan.common.annotation.LoginUser;
import com.feiyuan.common.entity.UserEntity;
import com.feiyuan.common.utils.R;
import com.feiyuan.movie.service.Search80SMovieService;
import com.feiyuan.movie.vo.MoviePlayerInfoVO;
import com.feiyuan.movie.vo.Search80SMovieVO;
import com.feiyuan.movie.vo.UserHistoryMovieVO;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 搜索80s影视信息
 */
@RestController
@RequestMapping("/app/movie/search")
public class Search80SMovieController {

    @Autowired
    private Search80SMovieService search80SMovieService;

    /**
     * 搜索
     * @param param 影视名
     * @return R
     */
    @GetMapping("/list")
    public R list(@RequestParam String param) {
        return R.ok(search80SMovieService.search(param));
    }

    /**
     * 查询80s视频的集数
     * @param movieVO 视频网址等信息
     * @return R
     */
    @PostMapping("/searchJi")
    @Login
    public R searchJi(@LoginUser UserEntity user, @RequestBody Search80SMovieVO movieVO) {
        return R.ok(search80SMovieService.searchJi(user.getUserId(), movieVO));
    }

    /**
     * 查询用户历史观看影视记录
     * @return ResponseUtils
     */
    @GetMapping("/searchUserMovie")
    @Login
    public R searchUserMovie(@LoginUser UserEntity user, @RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {
        return R.ok(search80SMovieService.searchUserMovie(user.getUserId(), pageIndex, pageSize));
    }

    /**
     * 通过影视历史播放ID查询影视集列表
     * @return ResponseUtils
     */
    @GetMapping("/searchByUserHistoryId")
    @Login
    public R searchByUserHistoryId(@LoginUser UserEntity user, @RequestParam("userHistoryId") Long userHistoryId) {
        return R.ok(search80SMovieService.searchByUserHistoryId(user.getUserId(), userHistoryId));
    }

    /**
     * 用户返回了或者退出APP时需要记录当前播放到第几集和第几秒
     * @param info 视频的详细地址
     * @return ResponseUtils
     */
    @PostMapping("/updateUserHistoryPlayer")
    @Login
    public R updateUserHistoryPlayer(@LoginUser UserEntity user, @RequestBody MoviePlayerInfoVO info) {
        return R.ok(search80SMovieService.updateUserHistoryPlayer(user.getUserId(), info));
    }

    /**
     * 移除用户的历史播放记录
     * @param info 视频的详细地址
     * @return ResponseUtils
     */
    @PostMapping("/removeUserMovie")
    @Login
    public R removeUserMovie(@LoginUser UserEntity user, @RequestBody UserHistoryMovieVO info) {
        return R.ok(search80SMovieService.removeUserMovie(user.getUserId(), info));
    }

    /**
     * 查询视频的详细的集数等数据
     * @param detailUrl 视频的详细地址
     * @return ResponseUtils
     */
    @GetMapping("/searchMovieInfo")
    @Login
    public R searchMovieInfo(@RequestParam("detailUrl") String detailUrl) {
        return R.ok(search80SMovieService.searchMovieInfo(detailUrl));
    }

    /**
     * 开始播放
     * @param collectionHref 播放地址
     * @return ResponseUtils
     */
    @GetMapping("/startPlay")
    @Login
    public R startPlay(@RequestParam("collectionHref") String collectionHref) {
        return R.ok(search80SMovieService.startPlay(collectionHref));
    }
}
